#include "main.h"

/*****************************
    STM32F103RET6

    ARM 32-bit Cortex-M3, 72MHz, 512kB Flash, 64kB SRAM,
    PLL, Embedded Internal RC 8MHz and 32kHz, Real-Time Clock,
    Nested Interrupt Controller, Power Saving Modes, JTAG and SWD,
    4 Synch. 16-bit Timers with Input Capture, Output Compare and PWM,
    2 16-bit Advanced Timer, 2 16-bit Basic Timer, 2 16-bit Watchdog Timers,
    SysTick Timer, 
    3 SPI/I2S, 
    2 I2C, 
    5 USART, 
    USB 2.0 Full Speed Interface,
    CAN 2.0B Active, 
    3 12-bit 16-ch A/D Converter, 
    2 12-bit D/A Converter, 
    SDIO, Fast I/O Ports
******************************/
uint16_t PointColor = 0; 
uint16_t x = 0, y = 0;
uint8_t  i = 0;
uint8_t  j = 0;
uint8_t icon = 0;
#define ShowLCD 1

int main()
{
    Sys_ClockInit();
    Sys_Timer3Init(); 
    Sys_Uart5Init(115200);
	
// 电源控制部分
    Sys_PowerInit();
    Delay_ms(200);

	printf("start application\r\n");
	
// 进入正常运行模式
    if(Read_PK1() == 1)
    {
        Delay_ms(20);
        if(Read_PK1() == 1)
        {
            while(Read_PK1());
            SysParameter.RunS = Run;
        }
    }
    else
    {
        SysParameter.RunS = Standby;
    }
    
// LCD显示部分
    Delay_ms(50);
    SimulationLCD_Init();
        
    while(1)
    {
        if(SysParameter.RunS == Run && j != Run )  
        {        
            SimulationLCD_ShowRun();
            SysParameter.SideBeat = 5;
            
            // 电池电压ADC
            Sys_ADCInit();
        
            // 空气检测仪
            PM_Init();
            
            Delay_ms(1000);
            Power_BL(1);
            
            j = Run;
        }
        else if(SysParameter.RunS == Standby && j != Standby)
        {
            Power_BL(0);
            
            SimulationLCD_ShowBatteryShape();
            
            Delay_ms(1000);
            Power_BL(1);
            
            j = Standby;
        }
     
// 读取状态
        Sys_ReadIndicate();

        if(SysParameter.RunS == Run && j == Run)
        {
            // 侧边跳动，间隔1秒
            if(SysParameter.SideBeatTime > 1000) {
                if(SysParameter.SideBeat==1) {
                    SimulationLCD_PointColor = SimulationLCD_CYAN;
                    for(i = 0; i < 13; i++)
                        SimulationLCD_DrawLine( 0, i, 32, i);   
                    SysParameter.SideBeat = 2;                        
                }
                else if(SysParameter.SideBeat==2) {
                    SimulationLCD_PointColor = SimulationLCD_CYAN;
                    for(i = 15; i < 28; i++)
                        SimulationLCD_DrawLine( 0, i, 24, i); 
                    SysParameter.SideBeat = 3;                      
                }
                else if(SysParameter.SideBeat==3) {
                    SimulationLCD_PointColor = SimulationLCD_CYAN;
                    for(i = 30; i < 43; i++)
                        SimulationLCD_DrawLine( 0, i, 16, i);
                    SysParameter.SideBeat = 4;
                }
                else if(SysParameter.SideBeat==4) {
                    SimulationLCD_PointColor = SimulationLCD_CYAN;
                    for(i = 45; i < 58; i++)
                    SimulationLCD_DrawLine( 0, i, 8, i); 
                    SysParameter.SideBeat = 5;                    
                }
                else if(SysParameter.SideBeat==5) {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    for(i = 0; i < 13; i++)
                        SimulationLCD_DrawLine( 0, i, 32, i);
                    for(i = 15; i < 28; i++)
                        SimulationLCD_DrawLine( 0, i, 24, i);    
                    for(i = 30; i < 43; i++)
                        SimulationLCD_DrawLine( 0, i, 16, i);    
                    for(i = 45; i < 58; i++)
                        SimulationLCD_DrawLine( 0, i, 8, i); 
                    SysParameter.SideBeat = 1;
                }
                SysParameter.SideBeatTime = 0;
            }

            // 获取PM值
            PM_GetProtocol();
        
            // 获取电池电压/电池容量
            SysParameter.BatteryVoltage  = ADC_GetSimpleData();
            if(SysParameter.BatteryVoltage >= 3.58)        {
                if(SysParameter.BatteryVoltage>=4.16)
                    SysParameter.BatteryCapacity = 100;
                else if(SysParameter.BatteryVoltage>=4.08)
                    SysParameter.BatteryCapacity = 90;
                else if(SysParameter.BatteryVoltage>=3.97)
                    SysParameter.BatteryCapacity = 80;            
                else if(SysParameter.BatteryVoltage>=3.90)
                    SysParameter.BatteryCapacity = 70;
                else if(SysParameter.BatteryVoltage>=3.84)
                    SysParameter.BatteryCapacity = 60;
                else if(SysParameter.BatteryVoltage>=3.79)
                    SysParameter.BatteryCapacity = 50;
                else if(SysParameter.BatteryVoltage>=3.76)
                    SysParameter.BatteryCapacity = 40;
                else if(SysParameter.BatteryVoltage>=3.73)
                    SysParameter.BatteryCapacity = 30;
                else if(SysParameter.BatteryVoltage>=3.71)
                    SysParameter.BatteryCapacity = 20;
                else if(SysParameter.BatteryVoltage>=3.65)
                    SysParameter.BatteryCapacity = 10;
            }
            else
                SysParameter.BatteryCapacity = 0;

            // 显示实际数字部分    
            if(ShowAirQuality.PM1_0 != AirQuality.PM1_0)  {
                x = (uint16_t)(SimulationLCD.width/2/2-20);
                y = 260; 
                SimulationLCD_ShowPM1_0_Num( x, y, AirQuality.PM1_0);

                ShowAirQuality.PM1_0 = AirQuality.PM1_0;      
            }
            if(ShowAirQuality.PM2_5 != AirQuality.PM2_5)  {
                x = (uint16_t)(SimulationLCD.width/2-20);
                y = 80; 
                SimulationLCD_ShowPM2_5_Num( x, y, AirQuality.PM2_5); 

                ShowAirQuality.PM2_5 = AirQuality.PM2_5;      
            }       
            if(ShowAirQuality.PM10 != AirQuality.PM10)    {
                x = (uint16_t)(SimulationLCD.width/2/2*3-16);
                y = 260;   
                SimulationLCD_ShowPM10_Num( x, y, AirQuality.PM10);

                ShowAirQuality.PM10 = AirQuality.PM10;      
            }
        }
        
// 显示电池图标
        if(SysParameter.Charging == 1 && SysParameter.RunS != 0)      {
            if(SysParameter.ShowBatteryTime >= 1000)
            {
                if(icon == 0)
                {
                    SimulationLCD_PointColor = SimulationLCD_WHITE;
                    icon = 1;
                }
                else
                {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    icon = 0;
                }
                for(i = 12; i < 19; i++)
                    SimulationLCD_DrawLine( 202, i, 205, i);     
                for(i = 12; i < 19; i++)
                    SimulationLCD_DrawLine( 207, i, 210, i);
                for(i = 12; i < 19; i++)
                    SimulationLCD_DrawLine( 212, i, 215, i);
                for(i = 12; i < 19; i++)
                    SimulationLCD_DrawLine( 217, i, 220, i);
                
                SysParameter.ShowBatteryTime = 0;
            }
        }
        else if(SysParameter.RunS != 0)        {
            if(SysParameter.ShowBatteryTime >= 2000)  { 
                if( SysParameter.BatteryCapacity >= 80 )      {
                    SimulationLCD_PointColor = SimulationLCD_WHITE;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 202, i, 205, i);     
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 207, i, 210, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 212, i, 215, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 217, i, 220, i);                   
                }
                else if( SysParameter.BatteryCapacity >= 60 ) {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 202, i, 205, i);
                    SimulationLCD_PointColor = SimulationLCD_WHITE;                    
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 207, i, 210, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 212, i, 215, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 217, i, 220, i);                    
                }
                else if( SysParameter.BatteryCapacity >= 40 ) {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 202, i, 205, i);                                       
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 207, i, 210, i);
                    
                    SimulationLCD_PointColor = SimulationLCD_WHITE;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 212, i, 215, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 217, i, 220, i);                       
                }
                else if( SysParameter.BatteryCapacity >= 20 ) {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 202, i, 205, i);      
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 207, i, 210, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 212, i, 215, i);
                    SimulationLCD_PointColor = SimulationLCD_WHITE;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 217, i, 220, i);                       
                }   
                else if( SysParameter.BatteryCapacity >= 1 )  {
                    SimulationLCD_PointColor = SimulationLCD_BLACK;
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 202, i, 205, i);                 
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 207, i, 210, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 212, i, 215, i);
                    for(i = 12; i < 19; i++)
                        SimulationLCD_DrawLine( 217, i, 220, i);                       
                }                
                SysParameter.ShowBatteryTime = 0;
            }
        }        
    }
}


